#include "esp32_uart.h"
#include <string.h>
#include <stdlib.h>

// 添加中断属性
void USART2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

// 私有变量
static uint8_t rx_buffer[FRAME_SIZE];  // 改用uint8_t类型
static volatile uint8_t rx_index = 0;
static volatile uint8_t rx_complete = 0;
static ESP32_Data_TypeDef esp32_data = {0, 0};

void ESP32_UART_Init(void) {
    // 使能UART2和GPIOA时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);  // UART2在APB1总线上
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);   // GPIOA在APB2总线上

    // GPIO配置
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = ESP32_RX_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(ESP32_GPIO_PORT, &GPIO_InitStructure);

    // UART配置
    USART_InitTypeDef USART_InitStructure;
    USART_InitStructure.USART_BaudRate = ESP32_BAUDRATE;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx;
    USART_Init(ESP32_UART_PORT, &USART_InitStructure);

    // 中断配置
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    USART_ITConfig(ESP32_UART_PORT, USART_IT_RXNE, ENABLE);
    USART_Cmd(ESP32_UART_PORT, ENABLE);
}

// 修改中断处理函数
void USART2_IRQHandler(void) {
    if (USART_GetITStatus(ESP32_UART_PORT, USART_IT_RXNE) != RESET) {
        uint8_t receivedByte = USART_ReceiveData(ESP32_UART_PORT);
        
        // 检查起始标记
        if (rx_index == 0) {
            if (receivedByte == START_MARKER) {
                rx_buffer[rx_index++] = receivedByte;
            }
        }
        // 接收数据和结束标记
        else if (rx_index < FRAME_SIZE) {
            rx_buffer[rx_index++] = receivedByte;
            
            // 检查是否接收完整帧
            if (rx_index == FRAME_SIZE) {
                if (rx_buffer[FRAME_SIZE-1] == END_MARKER) {
                    rx_complete = 1;
                }
                rx_index = 0;  // 重置接收索引
            }
        }
        
        USART_ClearITPendingBit(ESP32_UART_PORT, USART_IT_RXNE);
    }
}

// 修改数据处理函数
void ESP32_Data_Process(void) {
    if (rx_complete) {
        if (rx_buffer[0] == START_MARKER && rx_buffer[5] == END_MARKER) {
            // 直接存储原始字节，不进行解析
            esp32_data.raw_data[0] = rx_buffer[1];  // H高字节
            esp32_data.raw_data[1] = rx_buffer[2];  // H低字节
            esp32_data.raw_data[2] = rx_buffer[3];  // L高字节
            esp32_data.raw_data[3] = rx_buffer[4];  // L低字节
        }
        rx_complete = 0;
    }
}

// Get_Data函数保持不变
ESP32_Data_TypeDef ESP32_Get_Data(void) {
    return esp32_data;
}